package com.jiezhuo.utils;

import com.jiezhuo.entity.cmeduSchool.JzgNewMenu;

import java.util.*;
import java.util.stream.Collectors;

public class MeunUtils {

    public static ArrayList<JzgNewMenu> getJzgNewMenus(ArrayList<JzgNewMenu> menuTree) {
        Map<Long, JzgNewMenu> collect = menuTree.stream().collect(Collectors.toMap(JzgNewMenu::getId, (o) -> o));
//        Map<Long, JzgNewMenu> pidTree = objectMapper.readValue(objectMapper.writeValueAsString(collect), collect.getClass());
        for (JzgNewMenu mxkMenu : menuTree) {

            if (collect.containsKey(mxkMenu.getParentId())) {
                JzgNewMenu parentMenu = collect.get(mxkMenu.getParentId());

                if (parentMenu.getChildren() == null) {

                    ArrayList<JzgNewMenu> mxkMenus = new ArrayList<>();
                    mxkMenus.add(mxkMenu);
                    parentMenu.setChildren(mxkMenus);
                } else {
                    parentMenu.getChildren().add(mxkMenu);
                }
            }
        }


        ArrayList<JzgNewMenu> objects = new ArrayList<>();

        Set<Long> longs = collect.keySet();


        for (Long aLong : longs) {
            if (collect.get(aLong).getParentId() == 0) {
                objects.add(collect.get(aLong));
            }

        }
        ArrayList<JzgNewMenu> collect1 =(ArrayList) objects.stream().sorted(Comparator.comparing(JzgNewMenu::getSort)).collect(Collectors.toList());
        return collect1;
    }
}
